import java.nio.charset.StandardCharsets;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

public class People {
    private String id;
    private String password;
    private String name;
    private String tele;
    private int type;//0为管理员,1为普通用户,-1为注销
    private ResultSet re;
    private Base ba;
    Scanner sc=new Scanner(System.in);
    public People(){
    }

    public People(String id, String password, String name, String tele, int type) {
        this.id=id;
        this.password = password;
        this.name=name;
        this.tele = tele;
        this.type=type;
    }
    public String getTele(){
        return this.tele;
    }

    public void setTele(String tele){
        this.tele=tele;
    }
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }


    //登录----------------------------
    public boolean  login() throws SQLException, ClassNotFoundException {
        Base ba=new Base();
        String str=null,id=null,password=null,name=null,sql=null,sql_=null;
        boolean flag=false;
        while(true) {
            System.out.println("1.账号登录   0.退出");
            str=sc.next();
            if (str.equals("1")) {
                //查询数据库
                flag=true;
                //sql="select* from user where id='%s';";
                System.out.println("账号登录");
                System.out.print("请输入账号:");
                id=sc.next();
                System.out.print("请输入密码:");
                password=sc.next();
                //sql_=String.format(sql,id);
            }else if(str.equals("0")){
                //退出
                return false;
            }else{
                System.out.println("非法输入");
            }
            //验证密码
            if(flag) {
                for (int i=3;i>0;) {
                    //re=sta.executeQuery(sql_);
                    re=ba.getUserData(id);
                    while (re.next()) {
                        if (re.getString("password").equals(password)) {
                            this.id = re.getString("id");
                            this.name = re.getString("name");
                            this.password = password;
                            this.tele=re.getString("tele");
                            this.type = re.getInt("type");
                            System.out.println("登录成功！");
                            return true;
                        }
                    }
                    i--;
                    if(i>0) {
                        System.out.println("密码错误!还有" + i + "次机会");
                        //光标移回第一行
                        //re.first();
                        System.out.print("请重新输入密码:");
                        password = sc.nextLine();
                    }
                }
                return false;
            }
        }
    }

    //切换对应用户类型
    public void loginUsertype(People p){
        this.id=p.id;
        this.name=p.name;
        this.password=p.password;
        this.type=p.type;
    }

    //注册-----还缺个检验用户名、密码、手机号的非法问题?
    public void register() throws InterruptedException, SQLException, ClassNotFoundException {
        ba=new Base();
        String name=null,password=null,tele=null,id=null;
        System.out.flush();
        System.out.println("注册-----------");
        while(true){
            System.out.print("请设置用户名:");
            name=sc.next();
            //判断非法-----------
            break;
        }
        System.out.print("请设置电话号码:");
        tele=sc.next();
        System.out.print("请设置密码:");
        password=sc.next();
        while(true) {
            System.out.print("请再次确定设置密码:");
            if(password.equals(sc.next())){
                break;
            }
            System.out.println("密码错误！");
        }
        //生成id
        int pid=Integer.parseInt(ba.getMaxUserId());
        pid++;
        this.id =Integer.toString(pid);
        this.name=name;
        this.type=1;//默认普通用户
        this.password=password;
        this.tele=tele;
        //改数据库
        ba.addUser(this);
    }
    public void home()throws InterruptedException, SQLException, ClassNotFoundException{}

}
//sql
/*
密码
update user set password='%s' where id='%s';

用户名
update user set name='%s' where id='%s';

注销
delete from user where id='%s';

 */